{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-12-30T11:17:12.299121Z",
     "start_time": "2024-12-30T11:17:12.282136Z"
    }
   },
   "source": [
    "import tensorflow as tf\n",
    "import warnings\n"
   ],
   "outputs": [],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-30T11:17:12.657692Z",
     "start_time": "2024-12-30T11:17:12.630695Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\n",
    "x = tf.Variable(3, name='x')\n",
    "y = tf.Variable(4, name='y')\n",
    "f = x * x * y + y + 2\n",
    "print(f)\n"
   ],
   "id": "255f5e018b18aeb1",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Tensor(\"add_3:0\", shape=(), dtype=int32)\n"
     ]
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-30T11:17:13.461341Z",
     "start_time": "2024-12-30T11:17:13.444340Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 在with块内部，session被设置为默认的session\n",
    "with tf.Session() as sess:\n",
    "    x.initializer.run()  # 等价于 tf.get_default_session().run(x.initializer)\n",
    "    y.initializer.run()\n",
    "    result = f.eval()  # 等价于 tf.get_default_session().run(f)\n",
    "\n",
    "print(result)"
   ],
   "id": "b14b8fae40d8119c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "42\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "be352b3f7e478b8a"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
